package com.imay.live.view.custom;

import android.content.Context;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.StyleRes;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.imay.live.R;
import com.tencent.rtmp.ui.TXCloudVideoView;

/**
 * Created by jeaman on 2017/5/6.
 */

public class LiveVideoView extends FrameLayout /*implements View.OnClickListener*/ {
    private TXCloudVideoView mAnchorVideoView;
    private TXCloudVideoView mLinkMicVideoView;
    private ViewGroup mLinkMicContainerView;
    private TextView mLinkMicNickTv;
    private View mCameraView;
    private View mCloseView;
    private boolean mInitLinkMicView;
//    private OnLinkMicClickListener mListener;

    public LiveVideoView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public LiveVideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LiveVideoView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public LiveVideoView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.view_live_video, this);
        mAnchorVideoView = (TXCloudVideoView) findViewById(R.id.live_video_vv_anchor_video);
        mLinkMicContainerView = (ViewGroup) findViewById(R.id.live_video_rl_link_mic_container);
//
//        mCameraView.setOnClickListener(this);
//        findViewById(R.id.live_video_iv_close).setOnClickListener(this);
    }

    private void initLinkMicView() {
        if (!mInitLinkMicView) {
            mInitLinkMicView = true;
            View linkMicView = inflate(getContext(), R.layout.view_live_link_mic, mLinkMicContainerView);
            mLinkMicVideoView = (TXCloudVideoView) linkMicView.findViewById(R.id.live_video_vv_link_mic_video);
            mLinkMicNickTv = (TextView) linkMicView.findViewById(R.id.live_video_tv_nick);
            mCameraView = linkMicView.findViewById(R.id.live_video_iv_camera);
            mCloseView = linkMicView.findViewById(R.id.live_video_iv_close);
        }
    }

    public void showLinkMicView(boolean isMainPublisher, String nick) {
        initLinkMicView();
        mLinkMicNickTv.setVisibility(isMainPublisher ? VISIBLE : GONE);
        mLinkMicNickTv.setText(nick);
        mCameraView.setVisibility(isMainPublisher ? GONE : VISIBLE);
        mLinkMicContainerView.setVisibility(VISIBLE);
        mLinkMicVideoView.requestLayout();
    }

    public void hideLinkMicView() {
        mLinkMicContainerView.setVisibility(GONE);
    }

    public TXCloudVideoView getVideoView() {
        return mAnchorVideoView;
    }

    public TXCloudVideoView getLinkMicVideoView() {
        initLinkMicView();
        return mLinkMicVideoView;
    }

    public View getCameraView() {
        initLinkMicView();
        return mCameraView;
    }

    public View getCloseView() {
        return mCloseView;
    }

    public void destroy() {
        if (mAnchorVideoView != null) {
            mAnchorVideoView.onDestroy();
        }
        if (mLinkMicVideoView != null) {
            mLinkMicVideoView.onDestroy();
        }
    }

    //点击事件由cocos处理
//    public void setOnLinkMicClickListener(OnLinkMicClickListener listener) {
//        mListener = listener;
//    }
//
//    @Override
//    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.live_video_iv_close:
//                if (mListener != null) {
//                    mListener.onStopLinkMicClick();
//                }
//                break;
//            case R.id.live_video_iv_camera:
//                if (mListener != null) {
//                    mListener.onCameraClick();
//                }
//                break;
//        }
//    }
//
//    public interface OnLinkMicClickListener {
//        void onCameraClick();
//        void onStopLinkMicClick();
//    }

}
